package com.platform.server.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.platform.server.dao.DiscountDao;
import com.platform.server.response.pojo.ReturnData;
import com.platform.server.service.DiscountService;
import com.szrca.ima.utils.StringTools;

@Transactional
@Service
public class DiscountServiceImpl implements DiscountService {

	@Autowired
	private DiscountDao discountDao;
	
	/**
	 * 我的优惠券
	 */
	@Override
	public ReturnData selectMyDiscount(String id,Integer pageSize, Integer pageNo) {
		if(StringTools.isEmpty(id)){
			return new ReturnData(12,"参数有误");
		}
		//PageHelper.startPage(pageNo, pageSize);	
		List<Map<String, Object>> lists = discountDao.selectMyDiscount(id);
		if(lists.size() > 0){
			PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(lists);
			return new ReturnData(lists,pageInfo.getPages() ,pageInfo.getTotal(),200,"成功");
		}
		return new ReturnData(100,"暂无数据");
	}

	/**
	 * 已使用的优惠券
	 */
	@Override
	public ReturnData selectUseDiscount(String userid,Integer pageSize, Integer pageNo) {
		if(StringTools.isEmpty(userid)){
			return new ReturnData(12,"参数有误");
		}
		//PageHelper.startPage(pageNo, pageSize);	
		Integer saveMoney = discountDao.selectSaveMoney(userid);
		List<Map<String, Object>> lists = discountDao.selectUseDiscount(userid);
		//查看优惠券的订单信息
		for(int i = 0;i < lists.size();i++){
			lists.get(i).put("saveMoney", saveMoney);
			List<Map<String, Object>> map = discountDao.selectOrderInfo(lists.get(i).get("id").toString());
			if(map.size() >0){
				lists.get(i).put("order_id", map.get(0).get("order_id"));
				lists.get(i).put("order_type", map.get(0).get("order_type"));
				lists.get(i).put("order_status", map.get(0).get("status"));
			}
		}
		if(lists.size() > 0){
			//PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(lists);
			return new ReturnData(lists,/*pageInfo.getPages() ,pageInfo.getTotal()*/200,"成功");
		}
		return new ReturnData(100,"暂无数据");
	}

}
